package com.customization.unionpay.market.util;

import com.alibaba.fastjson.JSONObject;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author sjm
 * @description 清算业务核算系统
 */
public class ClearBusAccSystemUtils {

    private static final Logger log = LoggerFactory.getLogger(ClearBusAccSystemUtils.class);

    /**
     * 发起业务记账
     * @param details
     * @param person
     * @return
     */
    public static String businessAccountBookkeeping(List<JSONObject> details,String person) throws Exception{
        // 服务名称
        String serviceId = com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "businessAccountBookkeepServiceId");
        JSONObject params = combinedParameters(details,person);
        log.info("请求参数："+params);
        return MagpieUtils.getClearBusAccSystem(params,serviceId);
    }


    /**
     * 获取整体参数
     * @param details
     * @param person
     * @return
     */
    private static JSONObject combinedParameters(List<JSONObject> details, String person){
        JSONObject bodys = new JSONObject();

        JSONObject body = new JSONObject();
        //    "authorizer" : "HBBS",                                      授权人	    authorizer
        body.put("authorizer",person);
        //    "handler" : "HBBS",                                         经办人
        body.put("handler",person);
        //    "reviewer" : "HBBS"                                         复核人
        body.put("reviewer",person);
        //    "bussKeyWrd" : "",                                           业务关键字	bussKeyWrd
        body.put("bussKeyWrd","");
        //    "finDate" : "",                                             补记账日期
        body.put("finDate","");
        //    "remark" : "",                                              附言
        body.put("remark","");
        body.put("details",details);

        JSONObject header = getHeader();
        bodys.put("header",header);
        bodys.put("body",body);
        return bodys;
    }

    /**
     * 获取header
     * @return
     */
    private static JSONObject getHeader(){
        String requestSysCode = PropUtils.BB_LOG.getPropValue("marketSubject", "requestSysCode");
        String uuid = UUID.randomUUID().toString();
        JSONObject header = new JSONObject();
        // "acctSetCode" : "00002",                                              清算核算系统账套号 00006
        header.put("acctSetCode","00006");
        // "requestDate" : "20210827",                                           格式:“YYYYMMDD”
        Date date = new Date();
        String yyyymmdd = new SimpleDateFormat("yyyyMMdd").format(date);
        header.put("requestDate",yyyymmdd);
        // "requestId" : "HBBS20210827112941ABCD270000002",
        header.put("requestId",uuid);
        // "requestSysCode" : "HBBS",                                            暂定 WEAVEROA
        header.put("requestSysCode",requestSysCode);
        // "requestTime" : "112941",                                             格式:“hhmmss"
        String hhmmss = new SimpleDateFormat("hhmmss").format(date);
        header.put("requestTime",hhmmss);
        // "requestType" : "S01"                                                 S01
        header.put("requestType","S01");
        return header;
    }
}
